Visual Basic MSComm Control

First Published Wednesday, 20 July 2005 Last update 12 Jan, 2007 at 18:25:10

C O N T E N T S

Products
What we have to offer..

Order Full version NOW
Unfortunately in light of the fact that only a small number of SCLA copies have been sold, I have to increase the price of the software.  This software is now $30 hopefully I will be able to make suffucient money to support Vista..

This software, can save you many hours in testing serial links. For a professional approach use the detailed results in your commissioning reports.

LATEST NEWS

The SmartBay product range has been released Contract I.T. is going to bring Cabling Certification found in IP networks to CCTV networks.

CUSTOMER COMMENTS

I work in the oil and gas industry doing automation engineering and installation. I will use the software to setup and test radio links for data communications to the various oil and gas fields.”
Wes Sutton
Marathon Oil Company


The application we are using the program for is to check the character delay and loss rate over an Ethernet/TCP tunnelling system.”
Dr. Chris Clotworthy
Senior Design Engineer
Smart Light Devices Ireland

Contract I.T. Sincerely recommends NOT using Internet Explorer, we feel that you will be much safer if you switch to another browser. Contract I.T. recommends you use Firefox you can download it here. Or if you would prefer to check our recomendation google this topic

Successfully Using Microsoft Visual Basic 6.0 for Serial Communications.

There are a lot of thirdparty tools that perform the same function as Microsofts MSComm ActiveX Control. One of the reasons for this is the difficulty to get working and lack of documentation that most people experience when they attempt to use the control.

In my experience with MSComm I have developed a few simple proceedures for successfully using the control. I have never needed a thirdparty tool, although I’m sure there are some that offer additional features and are better from a performance point of view.

Here is a sample function that works very well for me. Read through the function and then I’ll explain each of the lines below.

Setup port function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Private Function SetupPort(ByRef objCom As MSComm)
   With objCom
       .Break = False
       .DTREnable = True
       .EOFEnable = False
       .Handshaking = comNone
       .InBufferCount = 0
       .InBufferSize = MAX_PACKETSIZE
       .InputLen = MAX_PACKETSIZE
       .InputMode = comInputModeText
       .NullDiscard = False
       .OutBufferCount = 0
       .OutBufferSize = MAX_PACKETSIZE
       .ParityReplace = Chr(0)
       .RThreshold = 1
       .SThreshold = 0
       .Settings = "115200,N,8,1" 'default settings
   End With
End Function

Function description

  1. Line one is the function declaration, you will notice that I have used the ByRef option in the faunction argument list, this tells the function that we want to work with MSComm object passed to the function and not a new instance of the object.
  2. Working with the passed object reference, for you C programmers think of the ByRef objCom as a pointer to the single instance of the object.
  3. In some cases (old style communications protocols) we may want to communicate a pause or break in communications. This is rarely used due to it’s simplicity. So in this instance I’ve set the .Break property to false.
  4. I’ve set the DTREnable to true. This indicates to any connected equipment that the terminal (your PC) is alive and well (and ready to recieve data so make sure you are). This actually causes a physical pin on the serial port to be driven to -12VDC for RS-232C.
  5. .EOFEnable will cause the OnComm event to fire if the recieved character is an EOF character, this is a specially designated character in the ASCII table. I’ve disabled it because I have the recieve event OnComm fire when each character is recieved (not the best performance configuration but it works in this simple example and I’ve never had any issues). Also if you rely on the EOF character to terminate your packets and it gets corrupted then you may not see the end of the packet correctly. Also ASCII comms sucks, you want to be able to transfer binary data and ecapsulate it within packets that you know the structure of. If you do use EOF then you’ll be limited to 7bits per byte.
  6. This is perhaps the most important setting, i have disable handshaking, because thats how germs are spread, no seriously we want to use a simple three wire cable (GND, TX and RX) we don’t want the hassle of wiring in those other wires especially when our software communications protocol will handle the start and stop of communications, similar to the MAC level in ethernet.
  7. This sounds ominous a buffer set to zero! but infact all it means is that your clearing any characters sitting in the objects recieve buffer, although I’m sure microsoft would have coded this into the ActiveX controls initialisation :P
  8. If you expect your PC to be under 100% load then it may be possible that Windows will miss a character at the serial port (although I’ve never seen it). In my configuration when a single character arrives the OnComm event fires and I retrieve the character. So the buffer theoretically never gets any fuller than 1 character, but to be safe I set the buffer to be slightly larger than the largest packet I expect to recieve.
  9. I set the .InputLen to be the same as the .InBufferSize this is a personal preference that I can’t remember why I do it I just know it’s good. Mayby it’s a karma thing I dunno. Specifically it’s the number of characters that are attempted to be retrieved when you use the objects Input method to get all the recieved characters. No matter you’ll only get what characters are in the buffer, no more no less.
  10. ComInputModeText works for me ComInputModeBinary doesn’t. If I use a hex editor program to look at the serial data all I’ve got is a bunch of 20h comming in.
  11. Let us not throw away 00h (00000000b) data instead let us keep it so that our protocol survives and prospers in a land where nohting can be something and indeed is.
  12. Use this to clear any character in the output buffer, as we arn’t using handshaking there should be no chars in the buffer but do you trust that your not sending “LAUNCH” to that nuclear submarine your controlling.
  13. Make the output buffer as large as you biggest packet so when you want to send your nicely structured data packet you can just plonk it into the Output method.
  14. Unless your using parity (FOR GODS SAKE DON’T USE IT) sorry uhm use CRC if your data integrity is important. Me I don’t care STOP/GO it’s all the same when controlling large machinery, things have to break sometime you may aswell be there to fix em. Set this property to whatever you want. CHR(0) may do the least damage if you forget or somehow enable parity.
  15. One is what you want here, now when a character is recieved you’ll know about ok. Set it to zero if you want a full recieve buffer and are planning on missing characters.
  16. Zero is cool here we know when chars are sent cause we aren’t using handshaking so they skedaddle as soon as we output em.
  17. This line is not really relevant unless your serial communications are relevant, duh it’s everything important in one line formatted as a string, what was microsoft thinking?
  18. Get a VB manual to understand this line
  19. Like </BODY> in a web page, so are the ends of our functions.

Ok you got all that?

How you’d use the function

MSComm1.CommPort = 1     ‘I am using COM 1 on my PC
SetupPort MSComm1        ‘Now I have setup the port
MSComm1.PortOpen = True  ‘Now I have opened the port and can output

You should trap for errors imediately after the PortOpen method incase the port is opened by another application or does not exist, here is how I do it.

If Err.Number = 0 Then
       msgbox "Hey it’s open!"
   Else
       msgbox "Doh it’s NOT open!”
End If

Now lets actually send a some data over the serial port

MSComm1.Output = "AT" & vbCrLf

Disclaimer

All the information on this page is provided for learning only, I’d expect anyone actually interfacing to equipment to ensure that they are acting in accordance with relevant Occupational Health and Safety regulations, and the recomendations of the equipment supplier/vender/manufacturer.

 

Free thumbnail preview by Thumbshots.org

 

Please contact our Webmaster with questions or comments.
© Copyright 2007 Contract I.T. Pty Limited.  All rights reserved.